import 'dart:typed_data';

/// 相册内项目类型
enum AlbumItemType {
  /// 照片
  photo,

  /// 视频
  video,
}

/// 相册模型
class Album {
  /// 相册ID（通常是路径的哈希值）
  final String id;

  /// 相册名称
  final String name;

  /// 相册路径
  final String path;

  /// 缩略图数据
  Uint8List? thumbnailData;

  /// 照片数量
  int photoCount;

  /// 视频数量
  int videoCount;

  /// 最后修改时间
  final DateTime lastModified;

  /// 是否已选择备份
  bool isSelected;

  Album({
    required this.id,
    required this.name,
    required this.path,
    this.thumbnailData,
    this.photoCount = 0,
    this.videoCount = 0,
    required this.lastModified,
    this.isSelected = false,
  });

  /// 获取总项目数
  int get totalCount => photoCount + videoCount;

  /// 根据开关状态获取有效项目数
  int getEffectiveCount({
    required bool autoBackupPhotos,
    required bool autoBackupVideos,
  }) {
    int count = 0;
    if (autoBackupPhotos) count += photoCount;
    if (autoBackupVideos) count += videoCount;
    return count;
  }

  /// 创建副本
  Album copyWith({
    String? id,
    String? name,
    String? path,
    Uint8List? thumbnailData,
    int? photoCount,
    int? videoCount,
    DateTime? lastModified,
    bool? isSelected,
  }) {
    return Album(
      id: id ?? this.id,
      name: name ?? this.name,
      path: path ?? this.path,
      thumbnailData: thumbnailData ?? this.thumbnailData,
      photoCount: photoCount ?? this.photoCount,
      videoCount: videoCount ?? this.videoCount,
      lastModified: lastModified ?? this.lastModified,
      isSelected: isSelected ?? this.isSelected,
    );
  }

  /// 从缓存JSON创建相册
  factory Album.fromJson(Map<String, dynamic> json) {
    return Album(
      id: json['id'],
      name: json['name'],
      path: json['path'],
      // 缩略图需要单独处理，不在这里解析
      photoCount: json['photoCount'] ?? 0,
      videoCount: json['videoCount'] ?? 0,
      lastModified: DateTime.parse(json['lastModified']),
      isSelected: json['isSelected'] ?? false,
    );
  }

  /// 转换为JSON以便缓存
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'path': path,
      // 缩略图需要单独处理，不在这里存储
      'photoCount': photoCount,
      'videoCount': videoCount,
      'lastModified': lastModified.toIso8601String(),
      'isSelected': isSelected,
    };
  }
}
